﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace LanDBom.Models
{
    public class Add_CL
    {
        LDbaseEntities LD = new LDbaseEntities();
        public int AddClirntele(Clirntele clirntele)
        {
            try
            {
                LD.Clirntele.Add(clirntele);
                return LD.SaveChanges();
            }
            catch (Exception ex)
            {
                var sunnd = ex.InnerException;
                throw;
            }
        }

        public string CJAPP( string REN="")
        {
            var ERP = LD.Applications.Where(s => s.AP_bian.Contains("ERP")).OrderByDescending(s => s.App_id).ToList();
            var diyi = ERP[0].App_id;
            var HNJ ="ERP"+ (diyi+1);
            var shun = new Applications()
            {
                AP_bian = HNJ,
                AP_ren = REN,
                App_bu="3",
                App_bei= "提交",
                App_BanB="1"
            };
            LD.Applications.Add(shun);
            LD.SaveChanges();
            return HNJ;
        }
        public string WULIAO(string liao="",string ERPliao="",string JMKL="")
        {
            var HNF = "";
            var shu = LD.Formation.Where(s => s.FM_bai == liao).FirstOrDefault();
            if (shu==null)
            {
                HNF = "0";
            }
            else
            {
                var HNh = new Formation()
                {
                    FM_bai = ERPliao,
                    FM_name = shu.FM_name,
                    MA_ID = shu.MA_ID,
                    FM_long = shu.FM_long,
                    FM_kuan = shu.FM_kuan,
                    FM_gao = shu.FM_gao,
                    FM_price = (decimal)double.Parse(JMKL),
                    PR_ID = shu.PR_ID,
                    FM_ji = shu.FM_ji,
                    FM_Zhuang = shu.FM_Zhuang,
                    FM_kong = shu.FM_kong,
                    FM_gg = shu.FM_gg,
                    FM_nyl = shu.FM_nyl,
                    FM_yj = shu.FM_yj,
                    FM_bmcl = shu.FM_bmcl,
                    FM_ycl = shu.FM_ycl,
                    FM_ms = shu.FM_ms,
                    FM_zx = shu.FM_zx,
                    FM_ej = shu.FM_ej,
                    FM_Jieti = shu.FM_Jieti,
                };
                LD.Formation.Add(HNh);
                LD.SaveChanges();
            }
            return HNF;
        }
        public string CJBOM(string Appbina="",string Bian="",string liao="")
        {
            var shnj = "";
            var shu = LD.BOMini.Where(s => s.BOM2_liao== Bian).FirstOrDefault();
            if (shu==null)
            {
                shnj = "0";
            }
            else
            {
                shu.BOM2_bian = Appbina;
                shu.BOM2_liao = liao;
                LD.BOMini.Add(shu);
                LD.SaveChanges();
                var klm = shu.BOM2_Gong;
                var SHUN = LD.operational.Where(S => S.OP_ID == klm).FirstOrDefault();
                if (SHUN !=null)
                {
                    shnj = SHUN.OP_price.ToString();
                }
            }
            return shnj;
        }
    }
}